# -*- coding: UTF-8 -*-
# @Author: lyw
# @Time: 2021/9/15 0015 10:18
# @Remark: windows 文件锁
import os
import sys

path = os.path.dirname(os.path.abspath(__file__))
path = os.path.dirname(path)
path = os.path.dirname(path)
path = os.path.dirname(path)
path = os.path.dirname(path)
path = os.path.dirname(path)

if not os.path.join(path, "python", "lib", "site-packages", "win32") in sys.path:
    sys.path.append(os.path.join(path, "python", "lib", "site-packages", "win32"))
if not os.path.join(path, "python", "lib", "site-packages", "win32", "lib") in sys.path:
    sys.path.append(os.path.join(path, "python", "lib", "site-packages", "win32", "lib"))
if not os.path.join(path, "python", "lib", "site-packages", "Pythonwin") in sys.path:
    sys.path.append(os.path.join(path, "python", "lib", "site-packages", "Pythonwin"))
import win32con
import pywintypes
import win32file

LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
LOCK_SH = 0  # The default value
LOCK_UN = 0  # The default value
LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
__overlapped = pywintypes.OVERLAPPED()


def flock(file, flags):
    hfile = win32file._get_osfhandle(file.fileno())
    if flags is LOCK_EX:
        win32file.LockFileEx(hfile, flags, 0, 0xffff0000, __overlapped)
    elif file is LOCK_UN:
        win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)
